package com.jokerhung.mymoney;

import android.app.Activity;
import android.os.Bundle;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;


public class login extends Activity {
	
	private EditText edtUsername;
	private EditText edtPassword;
	private TextView txtNewUser;
	private Button butLogin;
	
	private SQLiteDatabase db=null;
	private Cursor userCursor=null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
        this.edtUsername = (EditText) findViewById(R.id.Login_edtUsername);
        this.edtPassword = (EditText) findViewById(R.id.Login_edtPassword);
        this.butLogin = (Button) findViewById(R.id.Login_butLogin);
        this.txtNewUser = (TextView) findViewById(R.id.Login_txtNewuser);
        
        db = (new DatabaseHelper(this)).getWritableDatabase();
       
        this.butLogin.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		dologin();
            }
        });
        
        this.txtNewUser.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
                newUser();
            }
        });
        
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	
    	userCursor.close();
    	db.close();
    }
        
    public void dologin() {
    	String username = edtUsername.getText().toString();
    	String password = edtPassword.getText().toString();
    	    	
    	if (checkUser(username)) {
    		userCursor.moveToFirst();
    		if (userCursor.getString(1).equals(password)) {
    			Intent intent = new Intent("com.jokerhung.mymoney.VIEW_TRANS_LIST");
    			startActivity(intent);
    		} else {
    			Toast
					.makeText(this, "Wrong Password !", Toast.LENGTH_SHORT)
					.show();
    		}
    	} else {
    		Toast
				.makeText(this, "Invalid Username !", Toast.LENGTH_SHORT)
				.show();
    	}
    	
    }
    
    public void newUser() {
    	String username = edtUsername.getText().toString();
    	String password = edtPassword.getText().toString();
    	
    	if (checkUser(username)) {
    		Toast
				.makeText(this, "Username is existed !", Toast.LENGTH_SHORT)
				.show();
    	} else {
    		addUser(username, password);
    		Toast
				.makeText(this, "User added !", Toast.LENGTH_SHORT)
				.show();
    	}
    }
    
    public boolean checkUser(String username) {
    	userCursor = db.query("users", new String[]{"_id","password"}, 
    					"username=?", new String[]{username}, null, null, null);
    	if (userCursor.getCount() > 0) {
    		return true;
    	} else return false;
    }
    
    public void addUser(String username, String password) {
    	ContentValues values=new ContentValues(2);
		
		values.put("username", username);
		values.put("password", password);
		
		db.insert("users", "username", values);
		userCursor.requery();
    }
}